package com.zaimokuza.cn.controller;

import com.zaimokuza.cn.entity.PageResult;
import com.zaimokuza.cn.entity.enums.CustomizeExceptionEnum;
import com.zaimokuza.cn.entity.response.ResponseFormat;
import com.zaimokuza.cn.entity.user.*;
import com.zaimokuza.cn.service.SmsService;
import com.zaimokuza.cn.service.UserService;
import com.zaimokuza.cn.util.UserUtils;
import com.zaimokuza.cn.util.grace.GraceException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;

/**
 * @author zaimokuza
 */
@Api(tags = "用户相关")
@ResponseFormat
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    @ApiOperation(value = "登录", notes = "用于测试登录，实际处理交给spring-security")
    @PostMapping("/login")
    public void login(@RequestBody LoginUser loginUser) {
    }

    @ApiOperation(value = "登出", notes = "用于测试登出，实际处理交给spring-security")
    @GetMapping("/logout")
    public void logout() {
    }

    @ApiOperation(value = "注册用户", notes = "注册用户信息，对属性会进行校验")
    @PostMapping("/register")
    @Transactional(rollbackFor = Exception.class)
    public void register(@Validated @RequestBody RegisterUser registerUser) {
        userService.register(registerUser);
    }

    @ApiOperation(value = "更新用户信息", notes = "更新用户信息，对属性会进行校验")
    @PutMapping("/update")
    public void updateUser(@RequestBody DetailedUser detailedUser) {
        userService.updateUser(detailedUser);
    }

    @ApiOperation(value = "查询用户登录信息", notes = "查询用户登录信息")
    @GetMapping("/info")
    public LoginUser getUserLoginInfo() {
        // 获取用户信息时如果没有相应用户信息，则已经登录过期，提示并重新登录
        if (UserUtils.getUser() == null) {
            GraceException.display(CustomizeExceptionEnum.USER_LOGIN_TIME_OUT);
        }
        // 返回用户信息
        return userService.getUserLoginInfo(UserUtils.getUserId());
    }

    @ApiOperation(value = "根据ID查询用户简要信息", notes = "根据ID查询用户简要信息")
    @GetMapping("/brief/{id}")
    public BriefUser getBriefUserById(@PathVariable @ApiParam("用户ID") String id) {
        // 整理查询条件
        BriefUser param = new BriefUser();
        param.setId(id);
        // 查询用户简要信息
        return userService.getBriefUser(param, UserUtils.getUser() == null ? null : UserUtils.getUserId());
    }

    @ApiOperation(value = "根据域名查询用户简要信息", notes = "根据域名查询用户简要信息")
    @GetMapping("/domain/{domain}")
    public BriefUser getBriefUserByDomain(@PathVariable @ApiParam("用户域名") String domain) {
        // 整理查询条件
        BriefUser param = new BriefUser();
        param.setDomain(domain);
        // 查询用户简要信息
        return userService.getBriefUser(param, UserUtils.getUser() == null ? null : UserUtils.getUserId());
    }

    @ApiOperation(value = "查询用户详细信息", notes = "查询用户详细信息")
    @GetMapping("/detail")
    public DetailedUser getUserDetail() {
        return userService.getDetailedUser(UserUtils.getUserId());
    }

    @ApiOperation(value = "搜索用户", notes = "搜索用户")
    @GetMapping("/search")
    public ArrayList<BriefUser> getSearchUser(@RequestParam String name, @RequestParam(required = false) String time) {
        return userService.getSearchUserList(name, UserUtils.getUser() == null ? null : UserUtils.getUserId(), time);
    }

    @ApiOperation(value = "查询推荐用户", notes = "查询推荐用户")
    @GetMapping("/recommend")
    public ArrayList<BriefUser> getRecommendUser() {
        return userService.getRecommendUserList(UserUtils.getUser() == null ? null : UserUtils.getUserId());
    }

    @ApiOperation(value = "查询粉丝用户列表", notes = "根据域名查询粉丝用户列表")
    @GetMapping("/follower")
    public PageResult<BriefUser> getFollowerPage(@RequestParam @ApiParam("域名") String domain,
                                                 @RequestParam(defaultValue = "1") @ApiParam("当前页") Integer page,
                                                 @RequestParam(defaultValue = "10") @ApiParam("每页条数") Integer size) {
        return userService.getFollowerPage(domain, UserUtils.getUser() == null ? null : UserUtils.getUserId(), page, size);
    }

    @ApiOperation(value = "查询关注用户列表", notes = "根据域名查询关注用户列表")
    @GetMapping("/following")
    public PageResult<BriefUser> getFollowingPage(@RequestParam @ApiParam("域名") String domain,
                                                  @RequestParam(defaultValue = "1") @ApiParam("当前页") Integer page,
                                                  @RequestParam(defaultValue = "10") @ApiParam("每页条数") Integer size) {
        return userService.getFollowingPage(domain, UserUtils.getUser() == null ? null : UserUtils.getUserId(), page, size);
    }

    @ApiOperation(value = "查询联络人分页信息", notes = "查询联络人分页信息")
    @GetMapping("/contact")
    public PageResult<ContactUser> getContactUserPage(@RequestParam(defaultValue = "1") @ApiParam("当前页") Integer page,
                                                      @RequestParam(defaultValue = "10") @ApiParam("每页条数") Integer size) {
        return userService.getContactUserPage(UserUtils.getUserId(), page, size);
    }

    @ApiOperation(value = "查询用户分页信息", notes = "查询用户分页信息")
    @GetMapping("/page")
    public PageResult<BriefUser> getBriefUserPage(@RequestParam(required = false) @ApiParam("用户名称") String name,
                                                  @RequestParam(defaultValue = "1") @ApiParam("当前页") Integer page,
                                                  @RequestParam(defaultValue = "10") @ApiParam("每页条数") Integer size) {
        return userService.getBriefUserPage(name, UserUtils.getUser() == null ? null : UserUtils.getUserId(), page, size);
    }

    @Resource
    private SmsService smsService;

    @ApiOperation(value = "测试短信发送", notes = "测试短信发送")
    @GetMapping("/sendSms")
    public void test(@RequestParam @ApiParam("手机号") String phone, @RequestParam @ApiParam("验证码") String code) {
        smsService.sendVerificationCode(phone, code);
    }

}
